package com.yonyou.pmclouds.messagenotice.constructor.impl;

import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import com.yonyou.pmclouds.siteinspection.rmiitf.SiteInspectionQuery;
import com.yonyou.pmclouds.workquality.entity.WorkqualityVO;
import com.yonyou.pmclouds.workquality.service.rmiitf.WorkqualityQueryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class PatrolNoticeConstructor extends AbstractMessageNoticeConstructor {

    private final SiteInspectionQuery query;

    private final ProjectSimpleQuery projectSimpleQuery;

    private final WorkqualityQueryService service;

    @Autowired
    public PatrolNoticeConstructor(SiteInspectionQuery query, ProjectSimpleQuery projectSimpleQuery, WorkqualityQueryService service) {
        this.query = query;
        this.projectSimpleQuery = projectSimpleQuery;
        this.service = service;
    }

    @Override
    protected MessageNoticeVO[] generateContent(Entity vo) throws BusinessException {
        MessageNoticeVO noticeVO = coverBusinessVOToMessageVO(vo);
        if(noticeVO == null){
            return null;
        }

        List<MessageNoticeVO> noticeVOList = new ArrayList<>();

        SiteInspectionVO siteInspectionVO = query.queryOne(noticeVO.getRelateKey());
        ProjectVO projectVO = projectSimpleQuery.queryProject(siteInspectionVO.getPk_project());

        WorkqualityVO workqualityVO = service.queryByID(siteInspectionVO.getPk_inspectstandard());

        String prefix = "";

        //通知内容
        if(siteInspectionVO.getBillstatus() == (BillStatusConst.COMMON_APPROVED_STATUS)) {
            prefix = APPROVAL_SUFFIX_ADOPT_MESSAGE;
        }
        if(siteInspectionVO.getBillstatus() == (BillStatusConst.COMMON_REJECT_STATUS)) {
            prefix = APPROVAL_SUFFIX_REJECT_MESSAGE;
        }

        String messageContent = APPROVAL_PREFIX_MESSAGE + "[" + projectVO.getSname() + "]的+"+workqualityVO.getCheckname()+"+的质量巡查，" + prefix;

        //给发起人发送消息
        noticeVOList.add(getNoticeVO(siteInspectionVO, projectVO.getSname(), messageContent, siteInspectionVO.getCreator()));

        return noticeVOList.toArray(new MessageNoticeVO[0]);
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.PATROL_BILLTYPE;
    }

    /**
     * 生成一条消息通知
     *
     * @param vo
     * @param title
     * @param content
     * @param receiver
     * @return
     */
    private MessageNoticeVO getNoticeVO(SiteInspectionVO vo, String title, String content, String receiver) {
        MessageNoticeVO noticeVO = new MessageNoticeVO();
        noticeVO.setRelateType(vo.getBill_type());
        noticeVO.setRelateKey(vo.getPk_siteinspection());
        noticeVO.setMessageTitle(title);
        noticeVO.setMessageReceiver(receiver);
        noticeVO.setPkTenant(vo.getPk_tenant());
        noticeVO.setMessageContent(content);

        return noticeVO;
    }
}
